package com.yotoodesign.dianba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yotoodesign.dianba.R;
import com.yotoodesign.dianba.app.GlobalApplication;
import com.yotoodesign.dianba.bean.Tag;

public class TagAdapter extends BaseAdapter {
	private Context     mContext;
	private ArrayList<Tag> tags;
	
	/*
	 * 是否编辑状态
	 */
	private boolean editing = false;
	
	public TagAdapter(Context c){
		this.mContext = c;
	}
	
	@Override
	public int getCount() {
		return tags!=null?tags.size():0;
	}

	@Override
	public Object getItem(int position) {
		return tags!=null?tags.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if(editing){
			view = LayoutInflater.from(mContext).inflate(R.layout.tag_render_edit, null);
			
		}else{
			view = LayoutInflater.from(mContext).inflate(R.layout.tag_render, null);
		}
		
		final TextView tvTag = (TextView)view.findViewById(R.id.tvTag);
		tvTag.setText(tags.get(position).getName());
		
		final int p = position;
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(editing){
					tags.remove(p);
					notifyDataSetChanged();
					GlobalApplication.getInstance().deleteTag(new Tag(tvTag.getText().toString()));
				}
			}
		});
		
		return view;
	}

	public ArrayList<Tag> getTags() {
		return tags;
	}

	public void setTags(ArrayList<Tag> tags) {
		this.tags = tags;
	}

	public boolean isEditing() {
		return editing;
	}

	public void setEditing(boolean editing) {
		this.editing = editing;
	}

}
